/*******************************************************************************
 * @(#)DataAutoConfiguration.java 2018年10月02日 22:55
 * Copyright 2018 upbos.com. All rights reserved.
 *******************************************************************************/
package com.upbos.boot.sso.configuration;

import com.upbos.boot.sso.props.SsoConfigProps;
import com.upbos.boot.sso.props.CookieProps;
import com.upbos.sso.SsoManager;
import com.upbos.sso.cookie.CookieManager;
import com.upbos.sso.cookie.SimpleCookieManager;
import com.upbos.sso.filter.SsoFilter;
import com.upbos.sso.mgr.SsoManagerImpl;
import com.upbos.sso.plugin.Interceptor;
import com.upbos.sso.storage.StorageManager;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.List;

/**
 * <b>Application name：</b> DataAutoConfiguration.java <br>
 * <b>Application describing： </b> <br>
 * <b>Copyright：</b> Copyright &copy; 2018 upbos.com 版权所有。<br>
 * <b>Company：</b> upbos.com <br>
 * <b>Date：</b> 2018年10月02日 22:55 <br>
 * <b>author：</b> <a href="mailto:wangjz@miyzh.com"> wangjz </a> <br>
 * <b>version：</b>V4.1.0 <br>
 */

@Configuration
@EnableConfigurationProperties(value = {SsoConfigProps.class})
@ConditionalOnClass(SsoFilter.class)
public class SsoAutoConfiguration {

    @Resource
    private SsoConfigProps ssoConfigProps;


    @Bean
    public CookieManager cookieManager() {
        CookieManager cookieManager = new SimpleCookieManager();
        CookieProps cookieProps = ssoConfigProps.getCookie();
        if (cookieProps == null) {
            return cookieManager;
        }
        if (cookieProps.getTokenIdName() != null) {
            cookieManager.setTokenIdName(cookieProps.getTokenIdName());
        }
        if (cookieProps.getMaxAge() != null) {
            cookieManager.setMaxAge(cookieProps.getMaxAge());
        }

        if (cookieProps.getHttpOnly() != null) {
            cookieManager.setHttpOnly(cookieProps.getHttpOnly());
        }

        if (cookieProps.getRememberMe() != null) {
            cookieManager.setRememberMe(cookieProps.getRememberMe());
        }
        return cookieManager;
    }

    @Bean
    public SsoManager ssoManager(StorageManager storageManager, CookieManager cookieManager) {
        SsoManagerImpl ssoManager = new SsoManagerImpl();
        ssoManager.setStorageManager(storageManager);
        ssoManager.setCookieManager(cookieManager);
        return ssoManager;
    }


    @Bean
    public FilterRegistrationBean registerFilter(SsoManager ssoManager, List<Interceptor> interceptors) {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        SsoFilter ssoFilter = new SsoFilter();

        if (ssoConfigProps.getExcludeUrls() != null) {
            ssoFilter.setExcludeUrls(ssoConfigProps.getExcludeUrls());
        }
        if (ssoConfigProps.getLoginUrl() != null) {
            ssoFilter.setLoginUrl(ssoConfigProps.getLoginUrl());
        }
        if (ssoConfigProps.getLoginUrls() != null) {
            ssoFilter.setLoginUrls(ssoConfigProps.getLoginUrls());
        }

        ssoFilter.setSsoManager(ssoManager);
        ssoFilter.setInterceptors(interceptors);
        registration.setFilter(ssoFilter);
        registration.addUrlPatterns(ssoConfigProps.getFilterUrl());
        registration.setName(ssoConfigProps.getFilterName());
        registration.setOrder(ssoConfigProps.getOrder());
        return registration;
    }
}